package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.framework.web.service.TokenService;
import com.zzyl.nursing.domain.FamilyMember;
import com.zzyl.nursing.dto.PageMemberDto;
import com.zzyl.nursing.dto.UserLoginDto;
import com.zzyl.nursing.mapper.FamilyMemberMapper;
import com.zzyl.nursing.service.IFamilyMemberService;
import com.zzyl.nursing.service.WechatService;
import com.zzyl.nursing.vo.LoginVo;
import com.zzyl.serve.domain.NursingProject;
import com.zzyl.serve.service.INursingProjectService;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * 老人家属Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-22
 */
@Service
public class FamilyMemberServiceImpl extends ServiceImpl<FamilyMemberMapper, FamilyMember> implements IFamilyMemberService {
    @Autowired
    private FamilyMemberMapper familyMemberMapper;
    @Autowired
    private INursingProjectService nursingProject;


    @Autowired
    private WechatService wechatService;

    @Autowired
    private TokenService tokenService;

    private static List<String> NICK_NAME_PREFIX_LIST = Arrays.asList("钱途无量",
            "大桔大利",
            "日富一日",
            "好柿开花",
            "柿柿如意",
            "一椰暴富",
            "大柚所为",
            "杨梅吐气",
            "天生荔枝");

    @Override
    public LoginVo login(UserLoginDto dto) {
        //1. 获取用户openId
        String openId = wechatService.getOpenId(dto.getCode());

        //2. 判断用户，不存在则创建
        FamilyMember member = this.lambdaQuery().eq(FamilyMember::getOpenId, openId).one();
        if (member == null) {
            //注册一个 用户
            member = FamilyMember.builder()
                    .openId(openId)
                    .build();
            //打乱集合
            Collections.shuffle(NICK_NAME_PREFIX_LIST);
            member.setName(NICK_NAME_PREFIX_LIST.get(0) + RandomStringUtils.randomNumeric(4));
            this.save(member);
        }


        //3. 判断手机号，不存在则根据openId获取用户手机号
        if (StringUtils.isBlank(member.getPhone())) {
            String phone = wechatService.getPhone(dto.getPhoneCode());
            //4. 获取到手机号，就更新用户信息
            FamilyMember newFamilyMember = FamilyMember.builder().id(member.getId()).phone(phone)
                    .build();
            this.updateById(newFamilyMember);
        }
        //5. 生成令牌
        String token = tokenService.createToken(Map.of("userId", member.getId(), "nickName", member.getName()));

        //6. 返回数据


        return LoginVo.builder().nickName(member.getName()).token(token).build();
    }

    /**
     * 分页查询小程序端的护理项目列表
     *
     * @param pageMemberDto
     */
    @Override
    public List<NursingProject> queryList(PageMemberDto pageMemberDto) {
        List<NursingProject> list = nursingProject.list(Wrappers.<NursingProject>lambdaQuery()
                .like(pageMemberDto.getName() != null, NursingProject::getName, pageMemberDto.getName())
                .eq(pageMemberDto.getStatus() != null, NursingProject::getStatus, pageMemberDto.getStatus()));

        return list;
    }

    @Override
    public NursingProject getProject(Long id) {
        return nursingProject.getById(id);

    }
}
